package com.coocaa.ops.api.v1.internal.provider.impl;

import com.coocaa.ops.api.base.cache.enums.InterfaceCacheMethodEnum;
import com.coocaa.ops.api.base.cache.model.SimpleCacheKey;
import com.coocaa.ops.api.base.cache.service.SimpleCacheService;
import com.coocaa.ops.api.common.constant.PolicyGroupCategory;
import com.coocaa.ops.api.v1.content.biz.IPanelInfoBiz;
import com.coocaa.ops.api.v1.content.biz.ITabInfoBiz;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfo;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfoWithDmp;
import com.coocaa.ops.api.v1.internal.provider.ICacheUpdateProvider;
import com.coocaa.ops.common.core.biz.cache.constant.CacheTime;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-08-11
 */
@Service("tab")
public class TabCacheUpdateProviderImpl implements ICacheUpdateProvider {

    @Autowired
    private SimpleCacheService cacheService;

    @Autowired
    private ITabInfoBiz tabInfoBiz;

    @Autowired
    private IPanelInfoBiz panelInfoBiz;

    /**
     * 更新缓存-通过资源ID
     *
     * @param idStr
     * @return 缓存key列表
     */
    @Override
    public List<String> updateCacheByResourceId(String idStr) {
        Integer id = Integer.valueOf(idStr);
        List<String> keys = new ArrayList<>(5);
        TabInfoEntity tab = tabInfoBiz.getTabInfo(id, null);
        if (tab != null) {
            SimpleCacheKey tabCacheKey = getTabCacheKey(id);
            cacheService.set(tabCacheKey, tab, CacheTime.ONE_DAY);
            keys.add(tabCacheKey.toString());
            updateTabPanelRls(id, keys);
        }
        return keys;
    }

    public List<String> updateRls(Integer id) {
        List<String> keys = new ArrayList<>(5);
        updateTabPanelRls(id, keys);
        return keys;
    }

    private SimpleCacheKey getTabCacheKey(Integer tabId) {
        return SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getTabInfo)
                .withFilterKey("tabId", tabId);
    }

    private List<String> updateTabPanelRls(Integer tabId, List<String> cacheKeys) {
        List<PanelSimpleInfo> panelSimpleInfos = panelInfoBiz.getAllPanels(tabId, null);
        if (panelSimpleInfos != null) {
            SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getAllPanelsByTab)
                    .withFilterKey("tabId", tabId);
            cacheService.set(cacheKey, panelSimpleInfos, CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
        }
        updatePanelDmp(tabId, cacheKeys);
        return cacheKeys;
    }

    private void updatePanelDmp(Integer tabId, List<String> cacheKeys) {
        List<PanelSimpleInfoWithDmp> dmpPanelList = panelInfoBiz.getAllDmpPanels(tabId, PolicyGroupCategory.FORMAL);
        if (dmpPanelList != null) {
            SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getAllDmpPanels)
                    .withFilterKey("tabId", tabId);
            cacheService.set(cacheKey, dmpPanelList, CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
        }
    }
}
